package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.util.DateFormatUtils;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.exception.BssException;
import com.ztesoft.bss.common.util.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;

/**
 * Description:预约时间处理类
 * File Name:AppointmentTimeHandler
 * Modification History:
 * Date             Author      Version     Description
 * ------------------------------------------------------------------
 * 2021-10-26 9:52   邱萌         1.0       1.0 Version
 **/
@Service
public class AppointmentTimeHandler  extends AbstractAttrHandler {
    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    @Override
    public void init(CommonAttrVO commonAttr) {
        String provinceCode = ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();
        String sceneInstId = KiteWebContext.getContext().getCurrentSceneInstId();
        KiteAttr attr = querySceneInstService.getCommonAttr(sceneInstId, commonAttr.getFieldName());
        if (KiteStringUtils.isNotEmpty(provinceCode) && KiteStringUtils.equals(provinceCode, LocalKeyConsts.PROVINCE_HEBEI)) {
            commonAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
            if (Objects.nonNull(attr)) {
                attr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
            }
        } else {
            commonAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
            if (Objects.nonNull(attr)) {
                attr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
            }
        }
        operateSceneInstService.setCommonAttr(sceneInstId, attr);
    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        String provinceCode = ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();
        KiteAttr attr = querySceneInstService.getCommonAttr(sceneInstId, fieldName);
        boolean isHebei = false;
        if (KiteStringUtils.isNotEmpty(provinceCode) && KiteStringUtils.equals(provinceCode, LocalKeyConsts.PROVINCE_HEBEI)) {
            isHebei = true;
            if (Objects.nonNull(attr)) {
                attr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
            }
        } else {
            if (Objects.nonNull(attr)) {
                attr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
            }
        }
        operateSceneInstService.setCommonAttr(sceneInstId, attr);
        //如果是初始化则赋值为空
        Map<String, Object> extParams = ContextUtil.getLoginInfo().getUserInfo().getExtParams();
        if (Objects.nonNull(extParams) && extParams.containsKey("nodeCode")) {
            return super.apply(sceneInstId, fieldName, "", "");
        }
        if (isHebei && !KiteStringUtils.isEmpty(value)) {
            if ((DateFormatUtils.compareDate(value, DateFormatUtils.getDateByParams(1,DateFormatUtils.DATE_FORMAT), DateFormatUtils.DATE_FORMAT) < 0)) {
                BssException.throwOut(KeyConsts.LOG_MODULE, "","预约时间需选当天之后的日期");
            }
        }
        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }
}
